/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PengemudiDetailForm.java
 *
 * Created on Jul 24, 2010, 10:09:42 PM
 */
package com.oi.eksekutif.ui.detail;

import com.oi.eksekutif.dao.PosisiDAO;
import com.oi.eksekutif.domain.Pengemudi;
import com.oi.eksekutif.domain.Posisi;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import org.slf4j.LoggerFactory;

/**
 *
 * @author jasoet
 */
public class PengemudiDetailForm extends javax.swing.JPanel {
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(PengemudiDetailForm.class);
    private PosisiDAO posisiDAO;
    private Pengemudi pengemudi;
    private Image imageFoto;

    public PosisiDAO getPosisiDAO() {
        return posisiDAO;
    }

    public void setPosisiDAO(PosisiDAO posisiDAO) {
        this.posisiDAO = posisiDAO;

        if (posisiComboBox != null) {
            posisiComboBox.setModel(new DefaultComboBoxModel(posisiDAO.findAll().toArray()));
            log.info("PosisiComboBox is Setted");
        }

    }

    @Override
    public void setEnabled(boolean enabled) {
        this.namaTextField.setEditable(enabled);
        this.nomerKPPTextField.setEditable(enabled);
        this.nomorKTPTextField.setEditable(enabled);
        this.nomorSimTextField.setEditable(enabled);
        this.nomorTelpTextField.setEditable(enabled);
        this.namaTextField.setEditable(enabled);
        this.tanggalAwalMasukDateChooser.setEnabled(enabled);
        this.alamatTextArea.setEditable(enabled);
        this.ubahFotoButton.setEnabled(enabled);
        this.posisiComboBox.setEnabled(enabled);
    }

    public Pengemudi getPengemudi() {
        this.pengemudi.setPosisi((Posisi) this.posisiComboBox.getSelectedItem());
        this.pengemudi.setNama(this.namaTextField.getText());
        this.pengemudi.setNomorKpp(this.nomerKPPTextField.getText());
        this.pengemudi.setNomorKtp(this.nomorKTPTextField.getText());
        this.pengemudi.setNomorSim(this.nomorSimTextField.getText());
        this.pengemudi.setNomorTelp(this.nomorTelpTextField.getText());
        this.pengemudi.setTanggalAwalMasuk(this.tanggalAwalMasukDateChooser.getDate());
        this.pengemudi.setAlamat(this.alamatTextArea.getText());

        ImageIcon imageIcon = (ImageIcon) fotoLabel.getIcon();
        byte[] data = null;
        if (imageIcon != null) {
            try {
                BufferedImage bi = (BufferedImage) imageIcon.getImage();
                ByteArrayOutputStream bas = new ByteArrayOutputStream();
                ImageIO.write(bi, "jpg", bas);
                data = bas.toByteArray();
            } catch (IOException ex) {
                log.warn("IOException when storing Image to Pengemudi Object " + ex.getMessage());
            }
        }
        this.pengemudi.setFoto(data);
        return pengemudi;
    }

    public void setPengemudi(Pengemudi pengemudi) {
        this.pengemudi = pengemudi;
        nomerKPPTextField.setText(this.pengemudi.getNomorKpp());
        namaTextField.setText(this.pengemudi.getNama());
        alamatTextArea.setText(this.pengemudi.getAlamat());
        posisiComboBox.setSelectedItem(this.pengemudi.getPosisi());
        nomorTelpTextField.setText(this.pengemudi.getNomorTelp());
        nomorKTPTextField.setText(this.pengemudi.getNomorKtp());
        nomorSimTextField.setText(this.pengemudi.getNomorSim());
        tanggalAwalMasukDateChooser.setDate(this.pengemudi.getTanggalAwalMasuk());

        if (this.pengemudi.getFoto() != null) {
            try {
                BufferedImage image = ImageIO.read(new ByteArrayInputStream(pengemudi.getFoto()));
                this.imageFoto = image;
                fotoLabel.setIcon(new ImageIcon(image));
            } catch (IOException ex) {
                log.warn("IOException on loading foto cause : " + ex.getMessage());
            }
        } else {
            try {
                BufferedImage image = ImageIO.read(getClass().getResourceAsStream("/com/oi/eksekutif/main/jasoet.jpg"));
                this.imageFoto = image;
                fotoLabel.setIcon(new ImageIcon(image));
                log.warn("Load default Foto");
            } catch (IOException ex) {
                log.warn("IOException on loading foto cause : " + ex.getMessage());
            }
        }
    }

    /** Creates new form PengemudiDetailForm */
    public PengemudiDetailForm() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loadFotoFileChooser = new javax.swing.JFileChooser();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        namaTextField = new javax.swing.JTextField();
        posisiComboBox = new javax.swing.JComboBox();
        nomorTelpTextField = new javax.swing.JFormattedTextField();
        nomerKPPTextField = new javax.swing.JFormattedTextField();
        nomorKTPTextField = new javax.swing.JFormattedTextField();
        nomorSimTextField = new javax.swing.JFormattedTextField();
        tanggalAwalMasukDateChooser = new com.toedter.calendar.JDateChooser();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        alamatTextArea = new javax.swing.JTextArea();
        reloadButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        fotoLabel = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        ubahFotoButton = new javax.swing.JButton();

        loadFotoFileChooser.setFileFilter(new LoadFotoFileFilter());

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Nomer KPP :");

        jLabel2.setText("Nama :");

        jLabel3.setText("Posisi :");

        jLabel4.setText("Nomer Telp :");

        jLabel5.setText("Nomer KTP :");

        jLabel6.setText("Nomer SIM :");

        jLabel7.setText("Awal Masuk :");

        posisiComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bravo", "Charli", "Charling" }));

        tanggalAwalMasukDateChooser.setDate(new Date());
        tanggalAwalMasukDateChooser.setDateFormatString("dd MMMM yyyy");

        jLabel8.setText("Alamat :");

        alamatTextArea.setColumns(20);
        alamatTextArea.setRows(5);
        jScrollPane1.setViewportView(alamatTextArea);

        reloadButton.setText("Reload");
        reloadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reloadButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(posisiComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(reloadButton))
                    .addComponent(nomerKPPTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tanggalAwalMasukDateChooser, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
                    .addComponent(nomorSimTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
                    .addComponent(nomorKTPTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
                    .addComponent(nomorTelpTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
                    .addComponent(namaTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE))
                .addGap(12, 12, 12))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nomerKPPTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(namaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(posisiComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(reloadButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(nomorTelpTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(nomorKTPTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(nomorSimTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tanggalAwalMasukDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        fotoLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        fotoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/oi/eksekutif/main/jasoet.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(fotoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(fotoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        ubahFotoButton.setText("Ubah Foto");
        ubahFotoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ubahFotoButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ubahFotoButton, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ubahFotoButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void ubahFotoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ubahFotoButtonActionPerformed
        int result = loadFotoFileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            try {
                File f = loadFotoFileChooser.getSelectedFile();
                BufferedImage img = ImageIO.read(f);
                this.imageFoto = img;
                this.fotoLabel.setIcon(new ImageIcon(this.imageFoto));
            } catch (IOException ex) {
                log.warn("IOException when Loading Foto From JFileChooser");
            }
        }

    }//GEN-LAST:event_ubahFotoButtonActionPerformed

    private void reloadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reloadButtonActionPerformed
        // TODO add your handling code here:
        
            posisiComboBox.setModel(new DefaultComboBoxModel(posisiDAO.findAll().toArray()));
            log.info("PosisiComboBox is Setted");
        
    }//GEN-LAST:event_reloadButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea alamatTextArea;
    private javax.swing.JLabel fotoLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JFileChooser loadFotoFileChooser;
    private javax.swing.JTextField namaTextField;
    private javax.swing.JFormattedTextField nomerKPPTextField;
    private javax.swing.JFormattedTextField nomorKTPTextField;
    private javax.swing.JFormattedTextField nomorSimTextField;
    private javax.swing.JFormattedTextField nomorTelpTextField;
    private javax.swing.JComboBox posisiComboBox;
    private javax.swing.JButton reloadButton;
    private com.toedter.calendar.JDateChooser tanggalAwalMasukDateChooser;
    private javax.swing.JButton ubahFotoButton;
    // End of variables declaration//GEN-END:variables

    private static class LoadFotoFileFilter extends FileFilter {

        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }

            if (f.getName().toLowerCase().endsWith(".jpg")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return "Only JPG Image File";
        }
    }
}
